package me.zfz.business.service;

import me.zfz.base.constant.IConstant;
import me.zfz.business.bean.entity.TNAttachment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.zfz.base.bean.SmartResponse;
import me.zfz.base.exception.DaoException;
import me.zfz.base.exception.ServiceException;
import me.zfz.base.service.impl.MgrServiceImpl;
import me.zfz.business.dao.impl.AttachmentDao;

import com.mixsmart.utils.StringUtils;

/**
 * 附件  Service
 * @author author
 */
@Service("attServ")
public class AttachmentService extends MgrServiceImpl<TNAttachment> {

	@Autowired
	private AttachmentDao attDao;

	/**
	 * 查找附件
	 * @param id
	 * @return
	 * @throws ServiceException
	 */
	public SmartResponse<TNAttachment> findAtt(String id) throws ServiceException {
		SmartResponse<TNAttachment> smartResp = new SmartResponse<TNAttachment>();
		try {
			if(StringUtils.isNotEmpty(id)) {
				TNAttachment  att = attDao.find(id);
				if(null != att) {
					smartResp.setResult(IConstant.OP_SUCCESS);
					smartResp.setMsg(IConstant.OP_SUCCESS_MSG);
					smartResp.setData(att);
				}
				att = null;
			}
		}catch (DaoException e) {
			throw new ServiceException(e.getMessage(),e.getCause());
		} catch (Exception e) {
			throw new ServiceException(e.getCause());
		}
		return smartResp;
	}


	/**
	 * 删除附件
	 * @param id
	 * @return
	 * @throws ServiceException
	 */
	public SmartResponse<String> delete(String id) throws ServiceException {
		SmartResponse<String> smartResp = new SmartResponse<String>();
		try {
			if(StringUtils.isNotEmpty(id)) {
				if(attDao.delete(id)) {
					smartResp.setResult(IConstant.OP_SUCCESS);
					smartResp.setMsg(IConstant.OP_SUCCESS_MSG);
				}
			}
		} catch (DaoException e) {
			throw new ServiceException(e.getMessage(),e.getCause());
		} catch (Exception e) {
			throw new ServiceException(e.getCause());
		}
		return smartResp;
	}

}
